//
//  FreeHeroViewController.m
//  LOL_PalmarHelper
//
//  Created by dllo on 15/6/19.
//  Copyright (c) 2015年 hantao. All rights reserved.
//

#import "FreeHeroViewController.h"

@interface FreeHeroViewController ()

@end

@implementation FreeHeroViewController


-(void)dealloc
{
    [_collect release];
    [_modelArray release];
    [super dealloc];



}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.modelArray = [NSMutableArray array];

    
    [self requestData];

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.minimumInteritemSpacing = 5;

    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flowLayout.itemSize = CGSizeMake((self.view.frame.size.width  - 20 - 5) / 2, ((self.view.frame.size.width  - 20 - 5) / 2) / 2);
    flowLayout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 20);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - self.navigationController.navigationBar.bounds.size.height - self.navigationController.tabBarController.tabBar.bounds.size.height - 20) collectionViewLayout:flowLayout];
    self.collect.backgroundColor = [UIColor whiteColor];
    self.collect.delegate = self;
    self.collect.dataSource = self;

    [self.collect registerClass:[HeroCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.collect registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerReuse"];
    [self.view addSubview:self.collect];
    [_collect release];
    

    self.collect.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(freshAction)];
    

    
    
}

-(void)freshAction
{

    
[self requestData];

}



-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{


    HeroModel *hero = [self.modelArray objectAtIndex:indexPath.row];
    
    NSString *enName = hero.enName;
    HeroDetailViewController *heroDetail = [[HeroDetailViewController alloc] init];
    heroDetail.enName = enName;
    [self.navigationController pushViewController:heroDetail animated:YES];


}





-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{

    if (kind == UICollectionElementKindSectionHeader) {
        
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerReuse" forIndexPath:indexPath];
        headerView.backgroundColor = [UIColor clearColor];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(3, 0, 375, 20)];
        label.font = [UIFont systemFontOfSize:13];
        label.textColor = [UIColor lightGrayColor];
        label.text = @"*每周五更新周免";
        [headerView addSubview:label];
        
        return headerView;
    }

    return nil;

}




-(void)requestData
{
    
    
    
   
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    HUD.labelText = @"HHZ小组正卖力加载中";
    [self.navigationController.view addSubview:HUD];
    [HUD show:YES];
    [HUD release];

    [[HeroBaseHandle ShareHeroHeroDate] openDB];
    
    if ([[HeroBaseHandle ShareHeroHeroDate] selectAll] != nil) {
        self.modelArray = [[HeroBaseHandle ShareHeroHeroDate] selectAll];
    }
    
    
[AFN AFNGetData:@"http://lolbox.duowan.com/phone/apiHeroes.php?type=free" andBlock:^(id respondes) {
    
  
    if (![respondes isKindOfClass:[@"error" class]]) {
      
        [[HeroBaseHandle ShareHeroHeroDate] dropHerotable];
        
        
        [self.modelArray removeAllObjects];
        
        NSMutableDictionary *dic = respondes;
        NSMutableArray *array = [dic objectForKey:@"free"];
        
        [[HeroBaseHandle ShareHeroHeroDate] createHeroTable];
        for (NSMutableDictionary *Dic in array) {
            [[HeroBaseHandle ShareHeroHeroDate] openDB];

            HeroModel *model = [[HeroModel alloc] init];
            [model setValuesForKeysWithDictionary:Dic];
            
            [self.modelArray addObject:model];
            [[HeroBaseHandle ShareHeroHeroDate] insertHero:model];
            [model release];
        }
      
        [self.collect reloadData];
        [HUD hide:YES];
        
    }
    
    else if ([respondes isEqualToString:@"error"]) {
        
        
        UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [self.view addSubview:al];
        [al show];
        [al release];
        
        [HUD hide:YES];
        
    }

    
    
}];

   
    [self.collect reloadData];
    [self.collect.header endRefreshing];

}






-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{

    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{


    return self.modelArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    HeroCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        
        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
        
     
    }];
    cell.model = [self.modelArray objectAtIndex:indexPath.row];
    
    
    return cell;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
